package com.example.sc.learn.service.impl;

import com.example.sc.learn.service.api.UserApi;
import com.example.sc.learn.dao.BiningDao;
import com.example.sc.learn.entity.BiningRoom;
import com.example.sc.learn.entity.EatRecord;
import com.example.sc.learn.entity.User;
import com.example.sc.learn.service.BiningBusinessService;
import com.example.sc.learn.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * Created by baipan
 * Date: 2019-03-30
 */
@Service
public class BiningBusinessServiceImpl implements BiningBusinessService {

    @Autowired
    private BiningDao biningDao;
    @Autowired
    private UserApi userApi;


    @Override
    public void insertUser(User user) throws Exception{
        ResponseData<Boolean> responseData = userApi.insertUser(user);
        if (!responseData.getData()){
            throw new Exception("插入用户失败");
        }
    }

    @Override
    public User selectByUserCode(String userCode) throws Exception{
        ResponseData<User> responseData = userApi.selectByUserCode(userCode);
        User user = responseData.getData();
        return user;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertBinBingRoom(BiningRoom entity) {
        entity.setId(UUID.randomUUID().toString());
        biningDao.insertBinBingRoom(entity);
    }

    @Transactional(readOnly = true)
    @Override
    public BiningRoom selectBiningRoomByCode(String biningCode) {

        return biningDao.selectBiningRoomByCode(biningCode);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertEatRecord(EatRecord eatRecord) {
        eatRecord.setId(UUID.randomUUID().toString());
        biningDao.insertEatRecord(eatRecord);
    }

    @Transactional(readOnly = true)
    @Override
    public List<EatRecord> queryBiningByUserCode(String userCode) {
        return biningDao.queryBiningByUserCode(userCode);
    }

    @Transactional(readOnly = true)
    @Override
    public List<EatRecord> queryBiningByBiningCode(String biningCode) {
        return biningDao.queryBiningByBiningCode(biningCode);
    }

    @Transactional(readOnly = true)
    @Override
    public List<BiningRoom> selectAllBiningRoom() {
        return biningDao.selectAllBiningRoom();
    }

    @Transactional(readOnly = true)
    @Override
    public List<EatRecord> selectEatRecordByBiningCode(String biningRoomCode) {
        return biningDao.selectEatRecordByBiningCode(biningRoomCode);
    }

    @Override
    public List<User> selectTopThreeUser(String bingRoomCode) {
        return biningDao.selectTopThreeUser(bingRoomCode);
    }
}
